//挖地雷 因为规定了（u,v) 中u<v,帮可简单动态规划
#include <iostream>
using namespace std;
const int MAXN=200;
int n;
int w[MAXN+1];
bool adj[MAXN+1][MAXN+1];
int maxw[MAXN+1];
int select[MAXN+1];

int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>w[i];
    int x,y;
    do{
        cin>>x>>y;
        if (x>0){
            adj[x][y]=true;
        }else break;
    }while(1);
    maxw[n]=w[n];
    for(int u=n-1;u>=1;u--){
        maxw[u]=w[u];
        for(int v=u+1;v<=n;v++)
            if (adj[u][v]&&maxw[v]+w[u]>maxw[u]){
                maxw[u]=maxw[v]+w[u];
                select[u]=v;
            }
    }
    int ans=maxw[n];
    int ans_u=n;
    for(int u=n-1;u>=1;u--) 
        if (maxw[u]>ans) {
            ans=maxw[u];
            ans_u=u;
        }
    
    cout<<ans_u;
    ans_u=select[ans_u];
    while(ans_u>0){
        cout<<'-'<<ans_u;
        ans_u=select[ans_u];
    }
    cout<<endl;
    cout<<ans;
}